<?php
require_once 'CMS/Util.php';
require_once 'CMS/User.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseAdminController.php';
require_once 'CMS/AdminCommon.php';

//Người tạo : Lại Ngọc Thái Châu - Team 0
//Module 	: ListGroupManager
//Chức năng	: Quản lý các nhóm user

class Admin_ListGroupUserController extends BaseAdminController {
	public function init(){
		parent::init();
		$this->_helper->layout()->setLayout('layoutadmin');
		$user = new User();
		$user->init();
		$user->check_login();
		$user->permission();
	}
	
	public function indexAction(){
		$session = new Zend_Session_Namespace('login.auth');
		if(isset($session->delete_group) && $session->delete_group!="" )
		{
			$this->view->success = $session->delete_group;
			unset($session->delete_group);
		}
		else if(isset($session->update_group) && $session->update_group!="" )
		{
			$this->view->success = $session->update_group;
			unset($session->update_group);
		}
		else if(isset($session->insert_group) && $session->insert_group!="" )
		{
			$this->view->success = $session->insert_group;
			unset($session->insert_group);
		}
		$tbUserGroup = new Admin_Model_GroupUser();
		$this->view->data_user = $tbUserGroup->selectALLUserGroup();
	}
	
	public function insertAction(){
		$session = new Zend_Session_Namespace('login.auth');
		$tbUserGroup = new Admin_Model_GroupUser();
		if (!isset($_GET['user_group_id'])) {
			$this->view->action = "/admin/listgroupuser/insert";
			$this->view->button = "Insert";
		} else {
			$this->view->action = "/admin/listgroupuser/insert?user_group_id=".$_GET['user_group_id'];
			$this->view->button = "Update";
		}
		  
    	$this->view->cancel ="/admin/listgroupuser";
		if (isset($_POST) && $_POST!=null) {
			
			if(isset($_GET['user_group_id']) && is_numeric($_GET['user_group_id']))
			{
				if(!isset($_POST['name'])||$_POST['name']=="" || $_POST['name']==null)
				{
					$this->view->warning ="You did not enter Group Name";
				}
				else 
				{
					$array_update = array(
					"group_id" => $_GET['user_group_id'],
					"group_name" => $_POST['name'],
					"action" => serialize($_POST['permission'])
					);
					$tbUserGroup->updateUserGroup($array_update);
					
					unset($session->permission);
					$session->update_group = "Updated success";
					Header( "Location: /admin/listgroupuser" );
				}
				$user_group_info = $tbUserGroup->selectUserGroup($_GET['user_group_id']);
				$temp_permis= unserialize($user_group_info[0]['action']);
			}
			else 
			{
				if(!isset($_POST['name'])||$_POST['name']=="" || $_POST['name']==null)
				{
					$this->view->warning ="You Did not enter Group Name";
				}
				else 
				{
					$array = array(
					"group_name" => $_POST['name'],
					"action" => serialize($_POST['permission'])
					);
					$tbUserGroup->insertUserGroup($array);
					
					$session->insert_group = "Inserted success";
	  				Header( "Location: /admin/listgroupuser" );
				}
			}
			
			
		}
		else if(isset($_GET['user_group_id']) && is_numeric($_GET['user_group_id']))
		{
				$user_group_info = $tbUserGroup->selectUserGroup($_GET['user_group_id']);
				$temp_permis= unserialize($user_group_info[0]['action']);
		}
		
		if (isset($this->request->post['name'])) {
			$this->view->name = $_POST['name'];
		} elseif (!empty($user_group_info)) {
			$this->view->name = $user_group_info[0]['group_name'];
		} else {
			$this->view->name = '';
		}
		
		$ignore = array(
			'admin/installwidget',
			'admin/uninstallwidget',
			'admin/login',
			'admin/error'
		);
		$temp=null;	
		$this->data['permissions'] = array();
		
		$files = glob(APPLICATION_PATH . '/modules/admin/controllers/*.php');

		$i=0;
		foreach ($files as $file) {
			$data = explode('/', dirname($file));
			
			$permission = "admin/".strtolower(basename($file, 'Controller.php'));
			
			if (!in_array($permission, $ignore)) {
				 $temp[$i]= $permission;
				 $i++;
			}
			
		}
		
		/**** START Load widget len****/
		//Start ------- Khởi tạo Parameter
		$path = APPLICATION_PATH."/widgets";
		$tree = dir($path);
		$count = 0;
		$tbSidebarModule = new Admin_Model_SidebarModule();
		//End ------- Khởi tạo Parameter
		
		//Start ------- Vòng lặp đọc folder widgets
		while($dir = $tree->Read()){
			if (is_dir($path."/".$dir)&&($dir!=".")&&($dir!="..")&&($dir!=".svn"))
			{
				$xmlpath = $path."/".$dir."/";
				$xmlconfig = new SimpleXMLElement($xmlpath."config.xml",null,true);//Read config.xml
				if($xmlconfig->type=="sidebar")
				{
					$module = $tbSidebarModule->selectModuleByCode($xmlconfig->name);//Check Exist in DB
					
					$widget  = array(
						"module_code" => $xmlconfig->name,
						"module_type" => $xmlconfig->type,
						"module_title" => "widgets/".$xmlconfig->titleDefault,
					);
					$widgets[] = $widget;
				}
				else
				{
					$module = $tbSidebarModule->selectModuleByCode($xmlconfig->name);//Check Exist in DB
					
					$contentwidget  = array(
						"module_code" => $xmlconfig->name,
						"module_type" => $xmlconfig->type,
						"module_title" => "widgets/".$xmlconfig->titleDefault,
					);
					$Contentwidgets[] = $contentwidget;
				}
			}
		}
		
		/********END Load Widget len*******/
		$this->view->widgets = $widgets;		
		$this->view->permissions = $temp;
		$this->view->currentpage = $Contentwidgets;
		
		if (isset($_POST['permission']['access'])) {
			$this->view->access = $_POST['permission']['access'];
		} elseif (isset($temp_permis['access'])) {
			$this->view->access = $temp_permis['access'];
		} else { 
			$this->view->access = array();
		}

		if (isset($_POST['permission']['modify'])) {
			$this->view->modify = $_POST['permission']['modify'];
		} elseif (isset($temp_permis['modify'])) {
			$this->view->modify  = $temp_permis['modify'];
		} else { 
			$this->view->modify = array();
		}
		
		if (isset($_POST['permission']['modify_widget'])) {
			$this->view->modify_widget = $_POST['permission']['modify_widget'];
		} elseif (isset($temp_permis['modify_widget'])) {
			$this->view->modify_widget  = $temp_permis['modify_widget'];
		} else { 
			$this->view->modify_widget = array();
		}
		
		if (isset($_POST['permission']['modify_content'])) {
			$this->view->modify_content = $_POST['permission']['modify_content'];
		} elseif (isset($temp_permis['modify_content'])) {
			$this->view->modify_content  = $temp_permis['modify_content'];
		} else { 
			$this->view->modify_content = array();
		}	
	}
	public function deleteAction()
	{
		$session = new Zend_Session_Namespace('login.auth');
		$tbUserGroup = new Admin_Model_GroupUser();
		if(isset($_POST['selected']) && $_POST['selected']!=null) {
			foreach($_POST['selected'] as $id)
			{
				$tbUserGroup->deleteUserGroup($id);
			}
		}
		
	  $session->delete_group = "Deleted success";	   
	  Header( "Location: /admin/listgroupuser" );
	}
}